﻿<!doctype html>
<head>
	<meta charset="utf-8" />
</head>
<body>
	<div>百度翻译</div>
	<input id="q" placeholder="请输入要翻译的词" />
	<button id="goTran">翻译</button>
	<hr>
	<p id="result"></p>



	<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>

	<script src="md5.js"></script>
	<script type="text/javascript">
		$(function () {

            var appid = '20190911000333715';
            var key = 'U7IIBJUTZoNKkeMbRwud';
			var salt = (new Date).getTime();
			var from = 'auto';
			var to = 'auto';

			//翻译
			$("#goTran").click(function () {
				var query = $("#q").val();
				var str1 = appid + query + salt + key;
				var sign = MD5(str1);
				$.ajax({
					url: 'https://api.fanyi.baidu.com/api/trans/vip/translate',
					type: 'get',
					dataType: 'jsonp',
					data: {
						q: query,
						appid: appid,
						salt: salt,
						from: from,
						to: to,
						sign: sign
					},
					success: function (data) {
						var res = data.trans_result[0].dst;
						$("#result")[0].innerHTML = res;
						console.log(res);
					}
				});
			})

			// 多个query可以用\n连接  如 query='apple\norange\nbanana\npear'
		})

	</script>
</body>